package rensoft.portfolio.client.lib.view;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;

import rensoft.gwt.client.view.View;

public class ButtonBehavior extends View<Widget> implements MouseOverHandler, MouseOutHandler,
    MouseDownHandler, MouseUpHandler {
  private static final String ACTIVE = "active";
  private static final String DOWN = "down";
  private static final String OVER = "over";
  private boolean active;

  public ButtonBehavior(IsWidget widget) {
    initWidget(widget.asWidget());
    addStyleName("button");
    addMouseOverHandler(this);
    addMouseOutHandler(this);
    addMouseDownHandler(this);
    addMouseUpHandler(this);
  }

  public void onMouseDown(MouseDownEvent event) {
    if (active) return;
    addStyleName(DOWN);
  }

  public void onMouseOut(MouseOutEvent event) {
    removeStyleName(OVER);
    removeStyleName(DOWN);
  }

  public void onMouseOver(MouseOverEvent event) {
    if (active) return;
    addStyleName(OVER);
  }

  @Override
  public void onMouseUp(MouseUpEvent event) {
    if (active) return;
    removeStyleName(DOWN);
  }

  public void setActive(boolean value) {
    setStyleName(ACTIVE, value);
    active = value;
  }
}
